浏览量 4454
2015/01/12 01:45
package com.lzw;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;
/**
* @作者 王梓
* 莫失莫忘 仙寿恒昌
* 2015年1月11日
* Email:277215243@qq.com
*/
public class Test extends RecursiveAction {
public static void main(String args[]) throws InterruptedException{
ForkJoinPool pool=new ForkJoinPool();
pool.submit(new Test(0,30));
pool.awaitTermination(2, TimeUnit.SECONDS);
pool.shutdown();
}
/* (non-Javadoc)
* @see java.util.concurrent.RecursiveAction#compute()
*/
private static final int threshold=5;
private int start ;
private int end ;
public Test(int start ,int end){
this.start =start ;
this.end =end ;
}
@Override
protected void compute() {
// TODO Auto-generated method stub
if(end -start <threshold){
for(int i =start ;i <end;i++){
System. out.println(Thread.currentThread().getName()+ " i value:"+i);
}
} else{
int middle =(start +end )/2;
Test t1= new Test(start ,middle );
Test t2= new Test(middle ,end );
t1.fork();
t2.fork();
}
}
}
上一篇 搜索 下一篇